/**
 * File Name   : UpdateDaoImpl.java
 * Description : 
 */
package com.hh.common.ibatis.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * DB更新操作Dao
 * 
 * @author 冯昌硕
 * @create 2012-12-17 下午11:24:09
 */
public class UpdateDaoImpl implements IUpdateDao {

    private static final Logger log = Logger.getLogger(UpdateDaoImpl.class);
    
    private SqlMapClient sqlMapClient;
    
    /**
     * 更新记录
     * @param sqlId 要执行的SQLID
     * @param paramObj SQL执行参数
     * @return 执行结果，影响的行数
     * @throws SQLException
     * @create 2012-12-17 下午11:28:13
     */
    public int update(String sqlId, Object paramObj) throws SQLException {
        return sqlMapClient.update(sqlId, paramObj);
    }
    
    /**
     * 插入记录
     * @param sqlId 要执行的SQLID
     * @param paramObj SQL执行参数
     * @return 执行结果 The primary key of the newly inserted row.
     * @throws SQLException
     * @create 2012-12-23 下午3:39:57
     */
    public Object insert(String sqlId, Object paramObj) throws SQLException {
        return sqlMapClient.insert(sqlId, paramObj);
    }
    
    /**
     * 删除记录
     * @param sqlId 要执行的SQLID
     * @param paramObj SQL执行参数
     * @return 删除影响行数
     * @throws SQLException
     * @create 2012-12-23 下午6:33:02
     */
    public int delete(String sqlId, Object paramObj) throws SQLException {
        return sqlMapClient.delete(sqlId, paramObj);
    }
    
    /**
     * 批处理方式删除若干条记录
     * @param sqlId 执行删除的SQLID
     * @param paramObjLst 参数集合
     * @return
     * @throws SQLException
     * @create 2012-12-23 下午8:50:39
     */
    public int batchDelete(String sqlId, List<Object> paramObjLst) throws SQLException {
        int result = -1;
        
        try {
            sqlMapClient.startTransaction();
            
            sqlMapClient.startBatch();
            
            if((paramObjLst == null) || (paramObjLst.size() <= 0)) {
                log.info("批量删除[" + sqlId + "]参数集合为空，不作处理");
                return result;
            }
            
            for(Object paramObj : paramObjLst) {
                delete(sqlId, paramObj);
            }
            
            result = sqlMapClient.executeBatch();
            sqlMapClient.commitTransaction();
        } catch (SQLException e) {
            log.error("[batchDelete] 批处理异常： " + e.toString());
            throw e;
        } finally {
            sqlMapClient.endTransaction();
        }
        
        return result;
        
    }
    
    /**
     * 批处理方式删除若干条记录
     * @param sqlId 执行删除的SQLID
     * @param paramObjArr 参数数组
     * @return
     * @throws SQLException
     * @create 2012-12-23 下午8:10:22
     */
    public int batchDelete(String sqlId, Object[] paramObjArr) throws SQLException {
        int result = -1;
        try {
            sqlMapClient.startTransaction();
            
            sqlMapClient.startBatch();
            
            if((paramObjArr == null) || (paramObjArr.length <= 0)) {
                log.info("批量删除[" + sqlId + "]参数数组为空，不作处理");
                return result;
            }
            
            for(Object paramObj : paramObjArr) {
                delete(sqlId, paramObj);
            }
            
            result = sqlMapClient.executeBatch();
            sqlMapClient.commitTransaction();
        } catch(SQLException e) {
            log.error("[batchDelete] 批处理异常： " + e.toString());
            throw e;
        } finally {
            sqlMapClient.endTransaction();
        }
        
        return result;
    }
    
    public void startTransaction() throws SQLException {
        sqlMapClient.startTransaction();
    }
    
    public void endTransaction() throws SQLException {
        sqlMapClient.endTransaction();
    }
    
    public void commitTransaction() throws SQLException {
        sqlMapClient.commitTransaction();
    }
    
    public void startBatch() throws SQLException {
        sqlMapClient.startBatch();
    }
    
    public int executeBatch() throws SQLException {
        return sqlMapClient.executeBatch();
    }

    public void setSqlMapClient(SqlMapClient sqlMapClient) {
        this.sqlMapClient = sqlMapClient;
    }
    
}
